Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Matthew 10 posts 31 karma points
    Aug 04, 2010 @ 18:30
    Matthew
    0

    Creating an XSLT to get any Image [parametrised] from any Page

    As the title suggest, I am trying to create an XSLT to be used in a macro, that the user can give it two parameters: basically the page you want to find the image from, and also the image name. The XSLT seems good, but I am getting an integer exception at the $thisImage parameter in GetMedia. Anyone can suggest a better way / or a functioning way please?

     

    Thanks beforehand!

     

    <?xml version="1.0" encoding="UTF-8"?>
    
    <!DOCTYPE xsl:stylesheet [
      <!ENTITY nbsp "&#x00A0;">
    ]>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxml="urn:schemas-microsoft-com:xslt" xmlns:umbraco.library="urn:umbraco.library" exclude-result-prefixes="msxml umbraco.library">
      <xsl:output method="xml" omit-xml-declaration="yes" />
      <xsl:param name="currentPage" />
      <xsl:param name="searchPage" />
      <xsl:param name="imageName" />
      <xsl:template match="/">
        <xsl:variable name="rootNode" select="$currentPage/ancestor-or-self::node [@level=1]" />
        <xsl:variable name="thisPage" select="$rootNode/@nodeName [@alias=$searchPage]" />
        <xsl:variable name="thisImage" select="$thisPage/@nodeName/data [@alias=$imageName]"/>
        <xsl:element name="img">
          <xsl:attribute name="src">
            <xsl:value-of select="umbraco.library:GetMedia($thisImage, false) [@alias='umbracoFile']" />
          </xsl:attribute>
          <xsl:attribute name="alt">
            <xsl:value-of select="umbraco.library:GetMedia($thisImage, false) [@alias='altText']" />
          </xsl:attribute>
        </xsl:element>
      </xsl:template>
    </xsl:stylesheet>
  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Aug 04, 2010 @ 19:00
    Matt Brailsford
    0

    Hi Matthew,

    I'm assuming you are using Umbraco 4.5.x

    You'll want to change the thisImage variable to the following:

    <xsl:variable name="thisImage" select="$thisPage/@nodeName/$imageName"/>

    The data element is from the old 4.0.x schema

    Many thanks

    Matt

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Aug 04, 2010 @ 19:01
    Matt Brailsford
    0

    Ooops, you'll also want to change your src and alt selectors aswell

    umbraco.library:GetMedia($thisImage, false)/umbracoFile
    umbraco.library:GetMedia($thisImage, false)/altText

    Matt

  • Josh Townson 67 posts 162 karma points
    Aug 04, 2010 @ 19:04
    Josh Townson
    0

    Hi Matthew,

    I could be mistaken, but I don't think you can use the xsl:param to take custom parameters.

    <xsl:variable name="myVar" select="/macro/myVar"/>

    would be how I pull in settable paramaeters - and don't forget to define them on the macro too

    /Josh

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Aug 04, 2010 @ 21:30
    Chriztian Steinmeier
    0

    Hi Matthew,

    A couple of pointers for you:

    1) As Josh says, you have only one way of assigning values to <xsl:param> elements from the outside, which is

    to select content for them from the macro document Umbraco provides. So if your macro defines the parameters

    "searchPage" and "imageName", they're accessible as child nodes of the macro element:

    <xsl:param name="searchPage" select="/macro/searchPage" />
    <xsl:param name="imageName" select="/macro/imageName" />

    (You can use either xsl:param or xsl:variable here - makes no difference)

     

    2) About selecting the nodes you need (I don't know if you're using version 4.0 or 4.5, so I'll

    provide some "Universal Binary" code here.) - you need to do this:

    Selecting the "rootNode":

    - Use *[@level = 1] to have it work in both 4.0 and 4.5:

    <xsl:variable name="rootNode" select="$currentPage/ancestor-or-self::*[@level = 1]" />

    Selecting "thisPage":

    - It's a little bit confusing what you're actually sending into this - I'll assume an @id (contentPicker on macro);

    then you'd do this:

    <xsl:variable name="thisPage" select="$rootNode//*[@id = $searchPage]" />

    Selecting "thisImage":

    - I assume the image is a property on $thisPage:

    <xsl:variable name="thisImage" select="$thisPage/*[name() = $imageName]" />

     

    Yay - so far, so good. Now, generating output:

     

    3) First make sure you actually have a media id; then be sure to cache the GetMedia() result in a variable.

    Then, go generate the <img> tag in one swoop using Attribute Value Templates (curly braces):

    <xsl:if test="$thisImage">
        <xsl:variable name="imageData" select="umbraco.library:GetMedia($thisImage, false())" />
        <img src="{$imageData/*[@alias = 'umbracoFile' or self::umbracoFile]}" alt="{$imageData/*[@alias = 'altText' or self::altText]}" />
    </xsl:if>

     

    Important: If you're using version 4.5 (and not 4.5.1) you need to replace $imageData/* with $imageData//* because of a nasty bug...

    /Chriztian

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Aug 04, 2010 @ 21:54
    Chriztian Steinmeier
    0

    All of it, just for reference:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [
        <!ENTITY nbsp "&#x00A0;">
    ]>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:umbraco.library="urn:umbraco.library"
        exclude-result-prefixes="umbraco.library"
    >
        <xsl:output method="xml" omit-xml-declaration="yes" />
    
        <xsl:param name="currentPage" />
        <xsl:param name="searchPage" select="/macro/searchPage" />
        <xsl:param name="imageName" select="/macro/imageName" />
    
        <xsl:template match="/">
            <xsl:variable name="rootNode" select="$currentPage/ancestor-or-self::*[@level = 1]" />
            <xsl:variable name="thisPage" select="$rootNode//*[@id = $searchPage]" />
            <xsl:variable name="thisImage" select="$thisPage/*[name() = $imageName]" />
    
            <xsl:if test="$thisImage">
                <xsl:variable name="imageData" select="umbraco.library:GetMedia($thisImage, false())" />
                <img src="{$imageData/*[@alias = 'umbracoFile' or self::umbracoFile]}" alt="{$imageData/*[@alias = 'altText' or self::altText]}" />
            </xsl:if>
    
        </xsl:template>
    </xsl:stylesheet>

    /Chriztian

  • Matthew 10 posts 31 karma points
    Aug 05, 2010 @ 12:17
    Matthew
    0

    thanks alot for your reply Chriztian!

     

    that helped me alot.

     

    I have set up searchPage as a content picker (or should I use it as a text?) I have set up imageName as text. I am calling it like:

    <umbraco:Macro searchPage="1199" imageName="awardsImage" Alias="NG_Image_From_Page" runat="server"></umbraco:Macro>

     

    There are no errors, but nothing is being shown. any idea how I can debug please?

  • Matthew 10 posts 31 karma points
    Aug 05, 2010 @ 12:43
    Matthew
    1
        <xsl:variable name="thisImage" select="$thisPage/*[@alias = $imageName]" />

     

    did the trick ;)

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Aug 06, 2010 @ 00:33
    Chriztian Steinmeier
    0

    Hi Matthew,

    Yeah, you caught me - I forgot to "universalize" that one, but crossed my fingers and hoped you were using 4.5+ :-)

    Glad you got it, though ;-)

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft